home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / txtrsubs.c < prev    next >
C/C++ Source or Header  |  1993-10-13  |  25KB  |  928 lines

  1.  
  2.     /***********************************************
  3.     *
  4.     *    file d:\cips\txtrsubs.c
  5.     *
  6.     *    Functions: This file contains
  7.     *       sigma
  8.     *       skewness
  9.     *       amean
  10.     *       adifference
  11.     *       difference_array
  12.     *       hurst
  13.     *       compare
  14.     *       get_texture_options
  15.     *
  16.     *    Purpose:
  17.     *       These functions calculate measures
  18.     *       that help distinguish textures.
  19.     *
  20.     *    External Calls:
  21.     *       wtiff.c - round_off_image_size
  22.     *                 create_file_if_needed
  23.     *                 write_array_into_tiff_image
  24.     *       tiff.c - read_tiff_header
  25.     *       rtiff.c - read_tiff_image
  26.     *       edge.c - fix_edges
  27.     *       fitt.c - fit
  28.     *       filter.c - sort_elements
  29.     *
  30.     *    Modifications:
  31.     *       12 August 1993- created
  32.     *
  33.     *************************************************/
  34.  
  35. #include "cips.h"
  36.  
  37.  
  38.  
  39.      /*******************************************
  40.      *
  41.      *   sigma(..
  42.      *
  43.      *   This calculates the variance and the 
  44.      *   sigma for a sizeXsize area.
  45.      *
  46.      *   It sums the squares of the difference
  47.      *   between each pixel and the mean of
  48.      *   the area and divides that by the
  49.      *   number of pixels in the area.
  50.      *
  51.      *   The output image is set to the square
  52.      *   root of the variance since the variance
  53.      *   will almost certainly be out of range
  54.      *   for the image.  The square root of the
  55.      *   variance will be sigma.
  56.      *
  57.      *******************************************/
  58.  
  59. sigma(in_name, out_name, the_image, out_image,
  60.       il, ie, ll, le, size, threshold, high)
  61.    char   in_name[], out_name[];
  62.    int    il, ie, ll, le,
  63.           high, threshold, size;
  64.    short  the_image[ROWS][COLS],
  65.           out_image[ROWS][COLS];
  66. {
  67.    int      a, b, count, i, j, k,
  68.             max, mean, new_hi, new_low,
  69.             sd2, sd2p1;
  70.    short    sigma;
  71.    struct   tiff_header_struct image_header;
  72.    unsigned long diff, variance;
  73.  
  74.    sd2   = size/2;
  75.    sd2p1 = sd2 + 1;
  76.  
  77.    create_file_if_needed(in_name, out_name, out_image);
  78.  
  79.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  80.  
  81.    max     = 255;
  82.    new_hi  = 250;
  83.    new_low = 16;
  84.    if(image_header.bits_per_pixel == 4){
  85.        new_hi  = 10;
  86.        new_low = 3;
  87.        max     = 16;
  88.    }
  89.  
  90.       /***************************
  91.       *
  92.       *   Loop over image array
  93.       *
  94.       ****************************/
  95.  
  96.    printf("\n");
  97.    for(i=sd2; i<ROWS-sd2; i++){
  98.       if( (i%10) == 0) printf("%d ", i);
  99.       for(j=sd2; j<COLS-sd2; j++){
  100.  
  101.             /*****************************
  102.             *
  103.             *   Run through the small area
  104.             *   and calculate the mean.
  105.             *
  106.             ******************************/
  107.  
  108.          mean = 0;
  109.          for(a=-sd2; a<sd2p1; a++){
  110.             for(b=-sd2; b<sd2p1; b++){
  111.                mean = mean + the_image[i+a][j+b];
  112.             }
  113.          }
  114.          mean = mean/(size*size);
  115.  
  116.             /*****************************
  117.             *
  118.             *   Run through the small area
  119.             *   again and the calculate the
  120.             *   variance.
  121.             *
  122.             ******************************/
  123.  
  124.          variance = 0;
  125.          diff     = 0;
  126.          for(a=-sd2; a<sd2p1; a++){
  127.             for(b=-sd2; b<sd2p1; b++){
  128.                diff     = the_image[i+a][j+b] - mean;
  129.                variance = variance + (diff*diff);
  130.             }
  131.          }
  132.  
  133.          variance = variance/(size*size);
  134.          sigma    = sqrt(variance);
  135.          if(sigma > max) sigma = max;
  136.          out_image[i][j] = sigma;
  137.  
  138.       }  /* ends loop over j */
  139.    }  /* ends loop over i */
  140.  
  141.  
  142.      /* if desired, threshold the output image */
  143.    if(threshold == 1){
  144.        for(i=0; i<ROWS; i++){
  145.           for(j=0; j<COLS; j++){
  146.              if(out_image[i][j] > high){
  147.                   out_image[i][j] = new_hi;
  148.              }
  149.              else{
  150.                   out_image[i][j] = new_low;
  151.              }
  152.           }
  153.        }
  154.    }  /* ends if threshold == 1 */
  155.  
  156.    fix_edges(out_image, sd2);
  157.  
  158.    write_array_into_tiff_image(out_name, out_image,
  159.                                il, ie, ll, le);
  160.  
  161.  
  162. }  /* ends sigma */
  163.  
  164.  
  165.  
  166.  
  167.  
  168.      /*******************************************
  169.      *
  170.      *   skewness(..
  171.      *
  172.      *   This calculates the skewness for a
  173.      *   sizeXsize area.
  174.      *
  175.      *   Look at Levine's book page 449 for
  176.      *   the formula.
  177.      *   "Vision in Man and Machine" by
  178.      *   Martin D. Levine, McGraw Hill, 1985.
  179.      *
  180.      *******************************************/
  181.  
  182. skewness(in_name, out_name, the_image, out_image,
  183.          il, ie, ll, le, size, threshold, high)
  184.    char   in_name[], out_name[];
  185.    int    il, ie, ll, le,
  186.           high, threshold, size;
  187.    short  the_image[ROWS][COLS],
  188.           out_image[ROWS][COLS];
  189. {
  190.    int      a, b, count, i, j, k,
  191.             max, mean, new_hi, new_low,
  192.             sd2, sd2p1;
  193.    long     cube;
  194.    short    sigma, skew;
  195.    struct   tiff_header_struct image_header;
  196.    unsigned long diff, sigma3, variance;
  197.  
  198.    sd2   = size/2;
  199.    sd2p1 = sd2 + 1;
  200.  
  201.    create_file_if_needed(in_name, out_name, out_image);
  202.  
  203.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  204.  
  205.    max     = 255;
  206.    new_hi  = 250;
  207.    new_low = 16;
  208.    if(image_header.bits_per_pixel == 4){
  209.        new_hi  = 10;
  210.        new_low = 3;
  211.        max     = 16;
  212.    }
  213.  
  214.       /***************************
  215.       *
  216.       *   Loop over image array
  217.       *
  218.       ****************************/
  219.  
  220.    printf("\n");
  221.    for(i=sd2; i<ROWS-sd2; i++){
  222.       if( (i%10) == 0) printf("%d ", i);
  223.       for(j=sd2; j<COLS-sd2; j++){
  224.  
  225.             /*****************************
  226.             *
  227.             *   Run through the small area
  228.             *   and calculate the mean.
  229.             *
  230.             ******************************/
  231.  
  232.          mean = 0;
  233.          for(a=-sd2; a<sd2p1; a++){
  234.             for(b=-sd2; b<sd2p1; b++){
  235.                mean = mean + the_image[i+a][j+b];
  236.             }
  237.          }
  238.          mean = mean/(size*size);
  239.  
  240.             /*****************************
  241.             *
  242.             *   Run through the small area
  243.             *   again and the calculate the
  244.             *   variance and the cube.
  245.             *
  246.             ******************************/
  247.  
  248.          variance = 0;
  249.          diff     = 0;
  250.          cube     = 0;
  251.          for(a=-sd2; a<sd2p1; a++){
  252.             for(b=-sd2; b<sd2p1; b++){
  253.                diff     = the_image[i+a][j+b] - mean;
  254.                cube     = cube + (diff*diff*diff);
  255.                variance = variance + (diff*diff);
  256.             }
  257.          }
  258.  
  259.          variance        = variance/(size*size);
  260.          sigma           = sqrt(variance);
  261.          sigma3          = sigma*sigma*sigma;
  262.          if(sigma3 == 0)
  263.             sigma3       = 1;
  264.          skew            = cube/(sigma3*size*size);
  265.          out_image[i][j] = skew;
  266.          if(out_image[i][j] > max)
  267.             out_image[i][j] = max;
  268.  
  269.       }  /* ends loop over j */
  270.    }  /* ends loop over i */
  271.  
  272.  
  273.  
  274.      /* if desired, threshold the output image */
  275.    if(threshold == 1){
  276.        for(i=0; i<ROWS; i++){
  277.           for(j=0; j<COLS; j++){
  278.              if(out_image[i][j] > high){
  279.                   out_image[i][j] = new_hi;
  280.              }
  281.              else{
  282.                   out_image[i][j] = new_low;
  283.              }
  284.           }
  285.        }
  286.    }  /* ends if threshold == 1 */
  287.  
  288.    fix_edges(out_image, sd2);
  289.  
  290.    write_array_into_tiff_image(out_name, out_image,
  291.                                il, ie, ll, le);
  292.  
  293.  
  294. }  /* ends skewness */
  295.  
  296.  
  297.  
  298.  
  299.  
  300.      /*******************************************
  301.      *
  302.      *   amean(..
  303.      *
  304.      *   This calculates the mean measure
  305.      *   for a sizeXsize area.
  306.      *
  307.      *   Look at Levine's book page 451 for
  308.      *   the formula.
  309.      *   "Vision in Man and Machine" by
  310.      *   Martin D. Levine, McGraw Hill, 1985.
  311.      *
  312.      ******************************